package cn.datawin.bms.modules.deploy.controller;

import cn.datawin.bms.deploy.entity.DeplDockerInfoEntity;
import cn.datawin.bms.modules.deploy.service.DeplDockerInfoService;
import cn.datawin.common.core.util.R;
import cn.datawin.common.log.annotation.SysLog;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * docker容器表
 *
 * @author datawin
 * @date 2025-03-13 09:53:03
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/deploy/dockerInfo" )
@Tag(description = "dockerInfo" , name = "docker容器表管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class DeplDockerInfoController {

    private final DeplDockerInfoService deplDockerInfoService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param dockerInfo docker容器表
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('deploy_dockerInfo_view')" )
    public R getDockerInfoPage(@ParameterObject Page page, @ParameterObject DeplDockerInfoEntity dockerInfo) {
        LambdaQueryWrapper<DeplDockerInfoEntity> wrapper = Wrappers.lambdaQuery();
		wrapper.eq(Objects.nonNull(dockerInfo.getModuleId()), DeplDockerInfoEntity::getModuleId,dockerInfo.getModuleId());
		wrapper.eq(Objects.nonNull(dockerInfo.getBranchId()), DeplDockerInfoEntity::getBranchId,dockerInfo.getBranchId());
		wrapper.like(StrUtil.isNotBlank(dockerInfo.getContainerName()), DeplDockerInfoEntity::getContainerName,dockerInfo.getContainerName());
		wrapper.eq(StrUtil.isNotBlank(dockerInfo.getServerIp()), DeplDockerInfoEntity::getServerIp,dockerInfo.getServerIp());
		if(Objects.nonNull(dockerInfo.getStatus())){
			wrapper.eq(DeplDockerInfoEntity::getStatus,dockerInfo.getStatus());
		}else{
			wrapper.ne(DeplDockerInfoEntity::getStatus,3);
		}
		Page pageData = deplDockerInfoService.page(page, wrapper);
		List<DeplDockerInfoEntity> records = pageData.getRecords();
		deplDockerInfoService.wrapData(records);
		return R.ok(pageData);
    }


    /**
     * 通过id查询docker容器表
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('deploy_dockerInfo_view')" )
    public R getById(@PathVariable("id" ) Integer id) {
        return R.ok(deplDockerInfoService.getById(id));
    }

    /**
     * 激活docker容器
     * @param dockerInfo docker容器数据
     * @return R
     */
    @Operation(summary = "激活docker容器" , description = "激活docker容器" )
    @SysLog("激活docker容器" )
    @PostMapping("/active")
    @PreAuthorize("@pms.hasPermission('deploy_dockerInfo_active')" )
    public R active(@RequestBody DeplDockerInfoEntity dockerInfo) {
        return R.ok(deplDockerInfoService.activeContainer(dockerInfo));
    }


	/**
	 * 停止docker容器
	 * @param id docker容器id
	 * @return R
	 */
	@Operation(summary = "停止docker容器" , description = "停止docker容器" )
	@SysLog("停止docker容器" )
	@GetMapping("/stop")
	@PreAuthorize("@pms.hasPermission('deploy_dockerInfo_stop')" )
	public R stop(Integer id) {
		return R.ok(deplDockerInfoService.stopContainer(id));
	}


	/**
	 * 启用docker容器
	 * @param id docker容器id
	 * @return R
	 */
	@Operation(summary = "启用docker容器" , description = "启用docker容器" )
	@SysLog("启用docker容器" )
	@GetMapping("/start")
	@PreAuthorize("@pms.hasPermission('deploy_dockerInfo_start')" )
	public R start(Integer id) {
		return R.ok(deplDockerInfoService.restartContainer(id));
	}

	/**
	 * 废弃docker容器
	 * @param id docker容器id
	 * @return R
	 */
	@Operation(summary = "废弃docker容器" , description = "废弃docker容器" )
	@SysLog("废弃docker容器" )
	@GetMapping("/remove")
	@PreAuthorize("@pms.hasPermission('deploy_dockerInfo_remove')" )
	public R remove(Integer id) {
		return R.ok(deplDockerInfoService.removeContainer(id));
	}

	/**
	 * 清理docker容器
	 * @param id docker容器id
	 * @return R
	 */
	@Operation(summary = "清理docker容器" , description = "清理docker容器" )
	@SysLog("清理docker容器" )
	@GetMapping("/clean")
	@PreAuthorize("@pms.hasPermission('deploy_dockerInfo_clean')" )
	public R clean(Integer id) {
		return R.ok(deplDockerInfoService.cleanContainer(id));
	}


	/**
     * 修改docker容器表
     * @param dockerInfo docker容器表
     * @return R
     */
    @Operation(summary = "修改docker容器表" , description = "修改docker容器表" )
    @SysLog("修改docker容器表" )
    @PutMapping("/edit")
    @PreAuthorize("@pms.hasPermission('deploy_dockerInfo_edit')" )
    public R edit(@RequestBody DeplDockerInfoEntity dockerInfo) {
        return R.ok(deplDockerInfoService.editContainer(dockerInfo));
    }


	/**
	 * 复活docker容器
	 * @param id docker容器id
	 * @return R
	 */
	@Operation(summary = "复活docker容器" , description = "复活docker容器" )
	@SysLog("复活docker容器" )
	@GetMapping("/reborn")
	@PreAuthorize("@pms.hasPermission('deploy_dockerInfo_reborn')" )
	public R reborn(Integer id) {
		return R.ok(deplDockerInfoService.rebornContainer(id));
	}

	/**
	 * 升级docker容器
	 * @return R
	 */
	@Operation(summary = "升级docker容器" , description = "升级docker容器" )
	@SysLog("升级docker容器" )
	@PostMapping("/upgrade")
	@PreAuthorize("@pms.hasPermission('deploy_dockerInfo_upgrade')" )
	public R upgrade(@RequestBody DeplDockerInfoEntity dockerInfo) {
		return R.ok(deplDockerInfoService.upgradeContainer(dockerInfo));
	}

//    /**
//     * 通过id删除docker容器表
//     * @param ids id列表
//     * @return R
//     */
//    @Operation(summary = "通过id删除docker容器表" , description = "通过id删除docker容器表" )
//    @SysLog("通过id删除docker容器表" )
//    @DeleteMapping
//    @PreAuthorize("@pms.hasPermission('deploy_dockerInfo_del')" )
//    public R removeById(@RequestBody Integer[] ids) {
//        return R.ok(dockerInfoService.removeBatchByIds(CollUtil.toList(ids)));
//    }


//    /**
//     * 导出excel 表格
//     * @param dockerInfo 查询条件
//   	 * @param ids 导出指定ID
//     * @return excel 文件流
//     */
//    @ResponseExcel
//    @GetMapping("/export")
//    @PreAuthorize("@pms.hasPermission('deploy_dockerInfo_export')" )
//    public List<DockerInfoEntity> export(DockerInfoEntity dockerInfo,Integer[] ids) {
//        return dockerInfoService.list(Wrappers.lambdaQuery(dockerInfo).in(ArrayUtil.isNotEmpty(ids), DockerInfoEntity::getId, ids));
//    }
}
